//
//  ReceiveMainViewController.m
//  YKMediator_ReceiveModule
//
//  Created by linghit on 2021/8/17.
//

#import "ReceiveMainViewController.h"
#import "ReceiveViewModel.h"
#import <YKAlert/YKAlertCenter.h>
#import <Masonry/Masonry.h>
#import "ReceiveCellEnterViewController.h"
#import "ReceiveCellListViewController.h"

@interface ReceiveMainViewController ()
///
@property (nonatomic, strong, readwrite) ReceiveViewModel *viewModel;

///
@property (nonatomic, strong, readwrite) UIButton *cellButton;
///
@property (nonatomic, strong, readwrite) UIButton *collectionButton;
@end

@implementation ReceiveMainViewController

- (instancetype)initWithToken:(NSString *)token platform:(NSString *)platform comId:(NSString *)comId;
{
    self = [super init];
    if (self) {
        
        self.viewModel = [[ReceiveViewModel alloc] initWithToken:token platform:platform comId:comId];
//        self.refreshCommand = ((ReceiveViewModel *)self.viewModel).listCommand;
        [self.viewModel.errorSubject subscribeNext:^(NSError * _Nullable x) {
            [YKAlertCenter showMessage:x.localizedDescription];
        }];
        
//        [self registerWithClass:ReceiveMainCell.class identifier:NSStringFromClass(ReceiveMainCell.class)];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didSetupUI
{
    [super didSetupUI];
    self.title = @"收银系统";
    [self.view addSubview:self.cellButton];
    [self.cellButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view.mas_centerX);
        make.centerY.mas_equalTo(self.view.mas_centerY).offset(-140);
        make.size.mas_equalTo(CGSizeMake(200, 80));
    }];
    
    [self.view addSubview:self.collectionButton];
    [self.collectionButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view.mas_centerX);
//        make.centerY.mas_equalTo(self.view.mas_centerY).offset(140);
        make.top.mas_equalTo(self.cellButton.mas_bottom).offset(40);
        make.size.mas_equalTo(CGSizeMake(200, 80));
    }];
    
    
    
}

- (void)didBindData
{
    [super didBindData];
    @weakify(self);
    [[self.viewModel.userInfoCommand.executing skip:1] subscribeNext:^(NSNumber * _Nullable x) {
        if ([x boolValue]) {
            [YKAlertCenter showLoading:@"正在加载"];
        }else{
            [YKAlertCenter dissLoading];
        }
    }];
    [[self.cellButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self);
        ReceiveCellEnterViewController *vc = [[ReceiveCellEnterViewController alloc] initWithViewModel:self.viewModel];
        [self.navigationController pushViewController:vc animated:YES];
    }];
    
    [[self.collectionButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self);
        UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"提示" message:@"将进入收银界面您的任何操作都将被记录下来" preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            ReceiveCellListViewController *vc = [[ReceiveCellListViewController alloc] initWithViewModel:self.viewModel];
            [self.navigationController pushViewController:vc animated:YES];
        }];
        
        UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];
        
        [alertVC addAction:action];
        [alertVC addAction:cancel];
        [self presentViewController:alertVC animated:YES completion:nil];
    }];
    
//    [self.viewModel.userInfoCommand execute:nil];
}

- (UIButton *)cellButton
{
    if (!_cellButton) {
        _cellButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_cellButton setTitle:@"销售" forState:UIControlStateNormal];
        _cellButton.titleLabel.font = [UIFont systemFontOfSize:30 weight:UIFontWeightMedium];
        [_cellButton setTitleColor:UIColor.redColor forState:UIControlStateNormal];
        _cellButton.backgroundColor = UIColor.greenColor;
    }
    return _cellButton;
}

- (UIButton *)collectionButton
{
    if (!_collectionButton) {
        _collectionButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_collectionButton setTitle:@"收银" forState:UIControlStateNormal];
        _collectionButton.titleLabel.font = [UIFont systemFontOfSize:30 weight:UIFontWeightMedium];
        [_collectionButton setTitleColor:UIColor.redColor forState:UIControlStateNormal];
        _collectionButton.backgroundColor = UIColor.greenColor;
    }
    return _collectionButton;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
